package com.cn.wangxin.controller.user;


import com.cn.wangxin.annotation.SystemControllerLog;
import com.cn.wangxin.domain.requestmessage.RequestMessage;
import com.cn.wangxin.domain.user.User;
import com.cn.wangxin.service.user.DisplayService;
import com.cn.wangxin.util.JsonUtil;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by jing on 16/9/15.
 */
@Controller
@RequestMapping("/DisplayController")
public class DisplayController {
    private Logger logger = LoggerFactory.getLogger(DisplayController.class.getName());

    @Resource
    private DisplayService displayService;

    @SystemControllerLog(operationType="user",operationName = "测试")
    @RequestMapping(value="/display" ,method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody String helloWorld() {
        String message = displayService.getHelloWorld();
        logger.info(message);
        return message;
    }

    @RequestMapping(value="/random",method = {RequestMethod.GET, RequestMethod.POST},
            produces={"application/json;charset=UTF-8"})
    public @ResponseBody Map<String,Object> displayJson(HttpServletRequest httpServletRequest) throws Exception{
        String aa = httpServletRequest.getParameter("data");
        int randomValue = displayService.getRandomNumber();
        Map<String,Object> map = new HashMap<String,Object>();
        User user = displayService.getUser();
        map.put("random", randomValue);
        map.put("request","your request is "+aa);
        map.put("response",user);
        ObjectMapper objectMapper = new ObjectMapper();
        String result = objectMapper.writeValueAsString(map);
        logger.info("111sss:{}",map);
        return map;
    }

    @SystemControllerLog(operationType="user",operationName = "测试")
    @RequestMapping(value="/user",produces={"application/json;charset=UTF-8"},
            method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody List<User> displayUser(HttpServletResponse response) {
        try {
//            User user = displayService.getUser();
            User user = new User();
            user.setUsername("111");
            List<User> users = new ArrayList<User>();
            users.add(user);
            return users;
        }catch (Exception e){
                e.printStackTrace();
        }
        return null;

    }
    @RequestMapping(value = "/login",produces = {"application/json;charset=UTF-8"},
            method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody String login(HttpServletRequest httpServletRequest, @RequestBody String jsonObject)throws Exception{
        System.out.println(jsonObject);
        RequestMessage requestMessage = JsonUtil.json2Obj(jsonObject,RequestMessage.class);
        String name = httpServletRequest.getParameter("name");
        String pwd = httpServletRequest.getParameter("pwd") ;
        // TODO: 2016/9/20 判断账号密码是否正确
        String token = name+pwd+new Date().getTime();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",200);
        map.put("token",token);
        String result = JsonUtil.getObj2Json(map);
        return result;
    }
}
